<?php
class Admin_AdminController extends Zend_Controller_Action{
	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}
	
	public function init() {
		/* Initialize action controller here */
		Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$this->view->fullName = $infoUser->ADMINNAME;
		$this->view->role = $infoUser->ROLE_ID;
		$this->_model = new Admin_Model_DbTable_Admin();
	}
	
	public function indexAction(){
		$this->view->headTitle("Người Dùng","PREPEND");
		$this->view->title = "Người Dùng";
		$allU = $this->_model->getAllAdmin();
		$this->view->allUser = $allU;
		$upload = new Private_Upload();
		if($this->getRequest()->isPost()){
			$name = $this->_getParam("Name");
			$pass = md5("H0meVjet".md5($this->_getParam("Password")));
			$email = $this->_getParam("Email");
			$phone = $this->_getParam("Phone");
			$addr = $this->_getParam("Address");
			$role = $this->_getParam("role");
			$stt = $this->_getParam("Stt");
			$link = $this->_getParam("Link");
			if($_FILES["Img"]["name"]=="" and $link==""){
				$img = "/homeviet/public/uploaded/images/noavatar.png";
				$this->_model->addAdmin($role, $img, $name, $pass, $email, $phone, $addr, $stt);
				$this->_redirect("admin/admin");
			}elseif ($link){
				$img = $this->_getParam("Link");
				$this->_model->addAdmin($role, $img, $name, $pass, $email, $phone, $addr, $stt);
				$this->_redirect("admin/admin");
			}else{
				$img = "/homeviet/public/uploaded/images/".$_FILES["Img"]["name"];
				$upload->upload("user");
				$this->_model->updateAdmin($role, $img, $name, $pass, $email, $phone, $addr, $stt);
				$this->_redirect("admin/admin");
			}
		}
	}
	
	public function editAction(){
		$id = $this->_request->getParam("id");
		$this->view->headTitle("Sửa Người Dùng","PREPEND");
		$this->view->title = "Sửa Người Dùng";
		$upload = new Private_Upload();
		if($this->getRequest()->isPost()){
			$name = $this->_getParam("Name");
			$pass = md5("H0meVjet".md5($this->_getParam("Password")));
			$email = $this->_getParam("Email");
			$phone = $this->_getParam("Phone");
			$addr = $this->_getParam("Address");
			$role = $this->_getParam("role");
			$stt = $this->_getParam("Stt");
			$link = $this->_getParam("Link");
			if($_FILES["Img"]["name"]=="" and $link==""){
				$img = "";
				$this->_model->updateAdmin($id, $role, $img, $name, $pass, $email, $phone, $addr, $stt);
				$this->_redirect("admin/admin");
			}elseif ($link){
				$img = $this->_getParam("Link");
				$this->_model->updateAdmin($id, $role, $img, $name, $pass, $email, $phone, $addr, $stt);
				$this->_redirect("admin/admin");
			}else{
				$img = "/homeviet/public/uploaded/images/user/".$_FILES["Img"]["name"];
				$upload->upload("user");
				$this->_model->updateAdmin($id, $role, $img, $name, $pass, $email, $phone, $addr, $stt);
				$this->_redirect("admin/admin");
			}
		}else{
			$this->view->user = $this->_model->getAdminById($id);
		}
	}
	
	public function delAction(){
		$id = $this->_getParam("id");
		$this->_model->removeAdmin($id);
		$this->_redirect("admin/admin");
	}
	
	public function activeAction(){
		$id = $this->_getParam("id");
		$this->_model->activeAdmin($id);
		$this->_redirect("admin/admin");
	}
	
}